package com.zoe.onelink.core.autoconfigure;

import com.zoe.onelink.core.property.OnelinkProperties;
import com.zoe.onelink.core.util.HttpClientUtil;
import com.zoe.onelink.core.util.http.HttpClient;
import com.zoe.onelink.core.util.http.HttpClientInterceptor;
import com.zoe.onelink.core.util.http.OnelinkOkHttpClient;
import com.zoe.onelink.core.util.interceptor.HttpClientRateLimitInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2021-10-18
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(HttpClient.class)
public class OnelinkHttpClientAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(HttpClient.class)
    public HttpClient onelinkOkHttpClient(OnelinkProperties onelinkProperties) {
        return new OnelinkOkHttpClient(onelinkProperties.getHttp().getHttpClient());
    }

    @Bean
    @ConditionalOnBean(HttpClient.class)
    public HttpClientUtil httpRequestUtil(HttpClient httpClient) {
        return new HttpClientUtil(httpClient);
    }


    @Bean
    public HttpClientInterceptor httpClientRateLimitInterceptor(OnelinkProperties onelinkProperties) {
        return new HttpClientRateLimitInterceptor(onelinkProperties.getHttp().getHttpClient());
    }

}
